package com.ktjy.util;

import java.util.List;

public class Page<T> {
    //总页数
    private int totalPageCount=0;
    //页面大小
    private int pageSize=2;
    //记录总数
    private int totalCount;
    //当前页码
    private int currPageNo=1;
    //当页码的数据
    private List<T> list;
    //起始位置
    private int startRow;

    public Page(int totalCount,Object currPageNo,Object pageSize){
        Integer pagesize=Integer.valueOf(pageSize+"");
        Integer currPage=Integer.valueOf(currPageNo+"");
        this.setPageSize(pagesize);
        this.setTotalCount(totalCount);
        this.setCurrPageNo(currPage);
    }

    public int getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(int totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        if(pageSize>0){
            this.pageSize = pageSize;
        }else{
            this.pageSize=5;
        }
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        if(totalCount>0){
            this.totalCount = totalCount;
            //计算总页数
            totalPageCount=this.totalCount%this.pageSize==0?
                    this.totalCount/this.pageSize:
                    (this.totalCount/this.pageSize)+1;
        }else{
            this.totalCount=0;
            this.totalPageCount=0;
        }
    }

    public int getCurrPageNo() {
        if(totalPageCount<=0){
            this.currPageNo=1;
        }
        return currPageNo;
    }

    public void setCurrPageNo(int currPageNo) {
        if(this.totalCount!=0 && currPageNo>=this.totalPageCount){//页码超出总页数，重置为最后一页
            this.currPageNo=this.totalPageCount;
            //计算起始行
            this.startRow=(this.currPageNo-1)*this.pageSize;
        }else if(currPageNo>0){
            this.currPageNo = currPageNo;
            //计算起始行
            this.startRow=(this.currPageNo-1)*this.pageSize;
        }else{
            this.currPageNo=1;
        }
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }
}
